<?php
define('CC_Defender', 3);
define('TIMESTAMP', time());
$cdnpublic = '//cdn.bootcdn.net/ajax/libs/';


// 只执行一次
if (!defined('IN_CRONLITE')) {
	define('CACHE_FILE', 0);
	define('IN_CRONLITE', true);
	define('SYSTEM_ROOT', dirname(__FILE__) . '/');
	define('ROOT', dirname(SYSTEM_ROOT) . '/');
	define('SYS_KEY', 'quanquan');
	define('meihemb_ROOT', ROOT . '/meihe/meihemb/');
	date_default_timezone_set('PRC');
	$date = date('Y-m-d H:i:s');
	session_start();
	ob_start();
	header('Cache-Control: no-store, no-cache, must-revalidate');
	header('Pragma: no-cache');
	$is_defend = 0;
	$scriptpath = str_replace('\\', '/', $_SERVER['SCRIPT_NAME']);
	$sitepath = substr($scriptpath, 0, strrpos($scriptpath, '/'));
	$siteurl = ($_SERVER['SERVER_PORT'] == 0x1bb ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $sitepath . '/';
	// $authurl = ($_SERVER['SERVER_PORT'] == 0x1bb ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . '/';

	include_once SYSTEM_ROOT . 'function.php';
}

$gxbb = '{"code":200,"msg":"您当前已经是最新版，无需更新！","uplog":"暂无日志"}';
$gxbb = json_decode($gxbb, true);

$bbyz = '您当前已经是最新版,无需更新！';

// 检查站点是否已安装
if (!file_exists(ROOT . 'meihe/config.php')) {
	@header('Content-type:text/html;charset=utf-8');
	install();
	exit;
}

require ROOT . 'meihe/config.php';
require ROOT . 'meihe/version.php';
if (!$dbconfig['user'] || !$dbconfig['pwd'] || !$dbconfig['dbname']) {
	header('Content-Type: text/html; charset=UTF-8');
	install();
	exit;
}

// 数据库连接
include_once SYSTEM_ROOT . 'db.class.php';
$DB = new DB($dbconfig['host'], $dbconfig['user'], $dbconfig['pwd'], $dbconfig['dbname'], $dbconfig['port']);

if (!$DB->query('select * from meihe_config where 1')) {
	header('Content-Type: text/html; charset=UTF-8');
	install();
	exit;
}

include SYSTEM_ROOT . '../assets/css/cache.class.php';
$CACHE = new Cache();

$conf = $DB->select('SELECT * FROM `meihe_config`');
foreach ($conf as $key => $value) {
	unset($conf[$key]);
	$conf[$value['k']] = $value['v'];
}

// $conf = $CACHE->pre_fetch();

// 检查安全文件
if (!file_exists(ROOT . 'meihe/meihefh/meihe.lock') && file_exists(ROOT . 'meihe/meihefh/index.php')) {
	sysmsg('<h2>检测到无 meihe.lock 文件</h2><ul><li><font size="4">如果您尚未安装本程序，请<a href="/meihe/meihefh/">前往安装</a></font></li><li><font size="4">如果您已经安装本程序，请手动放置一个空的 meihe/meihefh/meihe.lock 文件到 meihe/meihefh 文件夹下，<b>为了您站点安全，在您完成它之前我们不会工作。</b></font></li></ul><br/><h4>为什么必须建立 meihe.lock 文件？</h4>它是防洪网的保护文件，如果检测不到它，就会认为站点还没安装，此时任何人都可以安装/重装防洪网。<br/><br/>', true);
	exit;
}

// 用户认证
$password_hash = '!@#%!strue';
include_once SYSTEM_ROOT . 'authcode.php';
$distid = $distid ?? '';
define('DIST_ID', hexdec($distid));
include_once SYSTEM_ROOT . 'meihemb.class.php';


// 检查用户登录状态
$islogin2 = 0;
if (isset($_COOKIE['user_token'])) {
	$token = authcode(daddslashes($_COOKIE['user_token']), 'DECODE', SYS_KEY);
	list($zid, $sid) = explode('	', $token);
	if ($userrow = $DB->get_row('select * from meihe_user where zid=\'' . intval($zid) . '\' limit 1')) {
		$session = md5($userrow['user'] . $userrow['pwd'] . $password_hash);
		if ($session === $sid && $userrow['active'] == 0) {
			$islogin2 = 1;
		}
	}
}

$islogin = 0;
if (!empty($_SESSION['admin_token'])) {
	// 管理员登录验证
	$token = authcode(daddslashes($_SESSION['admin_token']), 'DECODE', SYS_KEY);
	list($user, $sid) = explode('	', $token);
	$session = md5($conf['admin_user'] . $conf['admin_pwd'] . $password_hash);
	if ($session == $sid) {
		$islogin = 1;
	}
}

if ($islogin == 1) {
	$thtime = date('Y-m-d') . ' 00:00:00';
	// 执行统计查询
	$count5 = $DB->count("SELECT sum(money) from meihe_pay where tid!=-1 and addtime>='{$thtime}' and `status` = 1");
	$count6 = $DB->count('SELECT count(*) from meihe_user');
	$count7 = $DB->count("SELECT count(*) from meihe_user where addtime>='{$thtime}'");
	$fwjl = $DB->count('SELECT count(*) from meihe_log WHERE 1');
	$fwjr = $DB->count("SELECT count(*) from meihe_log where 1 and click_time>='{$thtime}'");
	$sczs = $DB->count('SELECT count(*) from meihe_report WHERE 1');
	$scjr = $DB->count("SELECT count(*) from meihe_report where 1 and date>='{$thtime}'");
	$vipyum = $DB->count('SELECT count(*) from meihe_list WHERE 1');
	$jrkthy = $DB->count("SELECT count(*) from meihe_list where 1 and date>='{$thtime}'");
	$fjyum = $DB->count('SELECT count(*) from meihe_list WHERE 1 and type=2');
	$hbmd = $DB->count('SELECT count(*) from meihe_hbmd WHERE 1');
	$hmd = $DB->count('SELECT count(*) from meihe_hbmd WHERE 1 and type=2');
	$duanljk = $DB->count('SELECT count(*) from meihe_api WHERE 1');
	$userrow['zid'] = isset($userrow['zid']) ? $userrow['zid'] : 0;
	$hyvip = $DB->count("SELECT count(*) from meihe_list WHERE 1 and upzid='{$userrow['zid']}'");
	$count12 = $DB->count("SELECT sum(money) FROM `meihe_pay` WHERE `type` = 'qqpay' AND `addtime` > '{$thtime}' AND `status` = 1");
	$count13 = $DB->count("SELECT sum(money) FROM `meihe_pay` WHERE `type` = 'wxpay' AND `addtime` > '{$thtime}' AND `status` = 1");
	$count14 = $DB->count("SELECT sum(money) FROM `meihe_pay` WHERE `type` = 'alipay' AND `addtime` > '{$thtime}' AND `status` = 1");
	$bbyz = '您当前已经是最新版，无需更新！';
} else {
}
// 防护机制
if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
	if (check_spider()) {
		return;
	}
	if (CC_Defender != 0) {
		include_once SYSTEM_ROOT . 'txprotect.php';
	}
}

if (!check_spider()) {
	if (CC_Defender == 2) {
		cc_defender();
	}

	if (CC_Defender == 3) {
		cc_defender_slide();
	}
}

require ROOT . 'vendor/autoload.php';
